package com.zrouter.plugin

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.MethodVisitor

class ZRouterMethodVisitor extends MethodVisitor {

    //注册表的全类名集合
    private List<String> routeList

    ZRouterMethodVisitor(MethodVisitor methodVisitor, List<String> routeList) {
        super(Opcodes.ASM5, methodVisitor)
        this.routeList = routeList
    }

    @Override
    void visitInsn(int opcode) {

        if (opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN) {
            routeList.each { name ->
                //插入函数的参数
                mv.visitLdcInsn(name)
                //插入方法
                mv.visitMethodInsn(
                        Opcodes.INVOKESTATIC,
                        //插入的方法所属的类
                        "com/study/zrouter/ZRouter",
                        //插入的方法名
                        "register",
                        //参数类型,无返回值
                        "(Ljava/lang/String;)V",
                        false
                )
            }
        }
        super.visitInsn(opcode)
    }

    @Override
    void visitMaxs(int maxStack, int maxLocals) {
        super.visitMaxs(maxStack + 4, maxLocals)
    }
}
